using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using GeoFramework.Gps;
using GeoFramework.Gps.Nmea;
using GeoFramework.Gps.Emulators;

namespace GeoFramework.Examples
{
    public partial class LoggingGpsDataForm : Form
    {
        public LoggingGpsDataForm()
        {
            InitializeComponent();

            // Bind the Playback SatelliteViewer to the satellites in the PlaybackInterpreter.  
            // This allows the SatelliteViewer to display the satellites during playback
            PlaybackSatelliteViewer.Satellites = PlaybackInterpreter.Satellites;

            // Tell the playback controls NOT to use live data from the GPS device.  This
            // lets us control the specific values of each control.
            PlaybackAltimeter.IsUsingRealTimeData = false;
            PlaybackCompass.IsUsingRealTimeData = false;
            PlaybackSpeedometer.IsUsingRealTimeData = false;
            PlaybackSatelliteViewer.IsUsingRealTimeData = false;
        }

        #region Recording Data to a Stream

        private void StartButton_Click(object sender, System.EventArgs e)
        {
            // Start receiving GPS Data
            RecordingInterpreter.Start();
            // Enable Pause and Stop buttons
            StartButton.Enabled = false;
            PauseButton.Enabled = true;
            StopButton.Enabled = true;
        }

        private void OutputFileBrowseButton_Click(object sender, System.EventArgs e)
        {
            SaveFileDialog1.FileName = "Logged GPS Data.txt";
            SaveFileDialog1.DefaultExt = ".txt";
            SaveFileDialog1.Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt";
            SaveFileDialog1.FilterIndex = 1;

            // If they cancelled, abort
            if (SaveFileDialog1.ShowDialog() == DialogResult.Cancel)
                return;

            // Remember the path of the output file
            OutputFileTextBox.Text = SaveFileDialog1.FileName;

            // Allow recording now that we have a file name
            StartRecordingButton.Enabled = true;
        }

        private void RecordingInterpreter_SentenceReceived(object sender, GeoFramework.Gps.Nmea.NmeaSentenceEventArgs e)
        {
            // Add the received sentence to the list box
            ReceivedSentencesListBox.Items.Add(e.Sentence.ToString());
            ReceivedSentencesListBox.SelectedIndex = ReceivedSentencesListBox.Items.Count - 1;
        }

        private void StopButton_Click(object sender, System.EventArgs e)
        {
            // Stop processing GPS data
            RecordingInterpreter.Stop();
            // Enable Start button
            StartButton.Enabled = true;
            PauseButton.Enabled = false;
            StopButton.Enabled = false;

        }

        private void PauseButton_Click(object sender, System.EventArgs e)
        {
            // Is the text "Pause" or "Resume"?
            if (PauseButton.Text == "Pause")
            {
                PauseButton.Text = "Resume";
                RecordingInterpreter.Pause();
            }
            else
            {
                PauseButton.Text = "Pause";
                RecordingInterpreter.Resume();
            }
        }

        private void StartRecordingButton_Click(object sender, System.EventArgs e)
        {
            // Start recording all data
            RecordingInterpreter.StartRecording(new FileStream(OutputFileTextBox.Text, FileMode.OpenOrCreate));
            // Enable Start button
            StartRecordingButton.Enabled = false;
            PauseRecordingButton.Enabled = true;
            StopRecordingButton.Enabled = true;
        }

        private void PauseRecordingButton_Click(object sender, System.EventArgs e)
        {
            // Is the text "Pause Recording" or "Resume Recording"?
            if (PauseRecordingButton.Text == "Pause Recording")
            {
                PauseRecordingButton.Text = "Resume Recording";
                // Temporarily suspend recording
                RecordingInterpreter.PauseRecording();
            }
            else
            {
                PauseRecordingButton.Text = "Pause Recording";
                // Temporarily suspend recording
                RecordingInterpreter.ResumeRecording();
            }
        }

        private void StopRecordingButton_Click(object sender, System.EventArgs e)
        {
            // Stop recording and close the file
            RecordingInterpreter.StopRecording();
            // Enable Start button
            StartRecordingButton.Enabled = true;
            PauseRecordingButton.Enabled = false;
            StopRecordingButton.Enabled = false;
        }

        private void RecordingInterpreter_SentenceRecorded(object sender, GeoFramework.Gps.Nmea.NmeaSentenceEventArgs e)
        {
            // Add the recorded sentence to the list box			 
            RecordedSentencesListBox.Items.Add(e.Sentence.ToString());
            RecordedSentencesListBox.SelectedIndex = RecordedSentencesListBox.Items.Count - 1;
        }

        #endregion

        #region Playing Back Logged Data

        private TextFileEmulator PlaybackStream;

        private void PlaybackInterpreter_AltitudeChanged(object sender, GeoFramework.DistanceEventArgs e)
        {
            // Set the altimeter to the current altitude
            PlaybackAltimeter.Value = e.Distance;
        }

        private void PlaybackInterpreter_BearingChanged(object sender, GeoFramework.AzimuthEventArgs e)
        {
            // Set the compass & satellite viewer to the current bearing
            PlaybackCompass.Value = e.Azimuth;
            PlaybackSatelliteViewer.Rotation = (Angle)e.Azimuth;
        }

        private void PlaybackInterpreter_SpeedChanged(object sender, GeoFramework.SpeedEventArgs e)
        {
            // Set the speedometer to the current speed
            PlaybackSpeedometer.Value = e.Speed;
        }

        private void PlaybackBrowseButton_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog1.FileName = "Logged GPS Data.txt";
            OpenFileDialog1.DefaultExt = ".txt";
            OpenFileDialog1.Filter = "All Files (*.*)|*.*|Text Files (*.txt)|*.txt";
            OpenFileDialog1.FilterIndex = 1;

            // If they cancelled, abort
            if (OpenFileDialog1.ShowDialog() == DialogResult.Cancel)
                return;

            // Remember the path of the output file
            PlaybackLogFileTextBox.Text = OpenFileDialog1.FileName;

            // Allow recording now that we have a file name
            PlaybackPlayButton.Enabled = true;

        }

        private void PlaybackPlayButton_Click(object sender, System.EventArgs e)
        {
            try
            {
                // Make a new NMEA stream using the file as its data source
                PlaybackStream = new TextFileEmulator(PlaybackLogFileTextBox.Text);
                // Assign this to the playback interpreter, which will process
                // the data.
                PlaybackInterpreter.BaseStream = PlaybackStream;
                // And start processing
                PlaybackInterpreter.Start();
                // Enable the Pause and Stop buttons
                PlaybackPlayButton.Enabled = false;
                PlaybackPauseButton.Enabled = true;
                PlaybackStopButton.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Unable to Play Back", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void PlaybackSpeed_Scroll(object sender, System.EventArgs e)
        {
            // Change the read interval of the playback stream
            PlaybackStream.ReadInterval = TimeSpan.FromMilliseconds(1001 - PlaybackSpeed.Value);
        }

        private void PlaybackStopButton_Click(object sender, System.EventArgs e)
        {
            // Stop the playback 
            PlaybackInterpreter.Stop();
            // Enable the Pause and Stop buttons
            PlaybackPlayButton.Enabled = true;
            PlaybackPauseButton.Enabled = false;
            PlaybackStopButton.Enabled = false;
        }

        private void PlaybackPauseButton_Click(object sender, System.EventArgs e)
        {
            if (PlaybackPauseButton.Text == "Pause")
            {
                // Pause playback
                PlaybackInterpreter.Pause();
                PlaybackPauseButton.Text = "Resume";
            }
            else
            {
                // Resume playback
                PlaybackInterpreter.Resume();
                PlaybackPauseButton.Text = "Pause";
            }
        }

        private void PlaybackInterpreter_SentenceInterpreted(object sender, GeoFramework.Gps.Nmea.NmeaSentenceEventArgs e)
        {
            // Add the sentence to the box
            PlaybackSentences.Items.Add(e.Sentence.ToString());
            PlaybackSentences.SelectedIndex = PlaybackSentences.Items.Count - 1;
        }
        #endregion
    }
}